const reqInterface = require("../api/models");
// 请求的api
export function getApi() {
  let api = '';
  switch (__wxConfig.envVersion) {
    case 'develop': // 开发服
      api = 'http://localhost:8080'
      return api;
      break;
    case 'trial': // 体验服
      api = 'http://localhost:8080'
      return api;
      break;
    default: // 正式服
      api = 'http://localhost:8080'
      return api;
  }
}


export function imagePrefix() {
  let prefix = '';
  switch (__wxConfig.envVersion) {
    case 'develop': // 开发服
      api = 'http://localhost:8080/'
      return api;
      break;
    case 'trial': // 体验服
      api = 'http://localhost:8080/'
      return api;
      break;
    default: // 正式服
      api = 'http://localhost:8080/'
      return api;
  }
}


export function findidx(arr, val) {
  return arr.findIndex(item => {
    return item.dictKey === val
  })
}


export function intersectionAlike(objA, objB) {
  console.log(objA, objB)

  const result = {};
  for (const keyA in objA) {
    if (objB.hasOwnProperty(keyA)) {
      result[keyA] = objB[keyA];
    }
  }
  return result;
}

export function isValidJson(jsonString) {
  try {
    JSON.parse(jsonString);
  } catch (e) {
    return false; // 捕获到错误，说明不是有效的JSON格式
  }
  return true; // 没有抛出错误，说明是有效的JSON格式
}

export function hasValue(obj) {
  let result = '';
  result = Object.keys(obj).filter(key => obj[key] !== 0).map(key => `${obj[key]}`).join('')
  return result;
}

export function divObj(obj) {
  const result = []
  obj.reduce((acc, curr, index) => {
    curr.idx = index;
    result.push(curr)
    // const key = `node${index}Info`;
    // acc[key] = curr;
    // return acc;
  }, {});

  return result;
}

export function alreadyValue(obj) {
  const result = []
  obj.reduce((acc, curr, index) => {
    curr.idx = index;
    result.push(curr)
    // const key = `node${index}Info`;
    // acc[key] = curr;
    // return acc;
  }, {});

  return result;
}

export function matchValue(val, num) {
  let self = this;
  return new Promise((resolve, reject) => {
    reqInterface.GetBladeSystemDictDictionary({
      code: val
    }).then(res => {
      res.findIndex(item => {
        if (item.dictKey == num) {
          resolve(item.dictValue)
        }
      })
    })
  })

}